import { Dic, RequestInstance } from "@/_interface";
import { inject, ref, watchEffect } from "vue";

export const useDicData = <T>(props: { option?: Dic<T> }) => {
  const request = inject<RequestInstance>("request");

  const loading = ref(false);
  const dicData = ref<T[]>();
  watchEffect(async () => {
    if (props.option?.dicUrl) {
      loading.value = true;
      try {
        dicData.value = await request?.get(props.option.dicUrl);
      } finally {
        loading.value = false;
      }
    } else {
      dicData.value = props.option?.dicData;
    }
  });

  return { dicData, loading };
};
